package com.lam.datasave;

import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.lam.fundamental.R;

public class SqliteActivity extends Activity{

	private Button bt_insertButton;
	@SuppressWarnings("unused")
	private Button bt_delTable;
	@SuppressWarnings("unused")
	private Button bt_newTable;
	@SuppressWarnings("unused")
	private Button bt_del;
	private Button bt_query;
	private static final String DATABASE_NAME="sqltest.db";
	private static final int DATABASE_VERSION= 3;
	private static final String TABLE_NAME="article";
	private static final String TITLE="title";
	private static final String BODY="body";
	DataBaseHelper dbh;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ds_sqlite);
		
		dbh=new DataBaseHelper(this);
		bt_insertButton= (Button)findViewById(R.id.bt_insertData);
		bt_del=(Button)findViewById(R.id.bt_delData);
		bt_query=(Button)findViewById(R.id.bt_query);
		bt_delTable=(Button)findViewById(R.id.bt_delTable);
		bt_newTable=(Button)findViewById(R.id.bt_newTable);
		
		bt_insertButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				SQLiteDatabase db = dbh.getWritableDatabase();
				String sql1="insert into "+ TABLE_NAME+"("+TITLE+","+BODY+
						") values('h','ni');";
				try {
					Log.i("insert data:", sql1);
					db.execSQL(sql1);
					setTitle("insert success");
				} catch (Exception e) {
					// TODO: handle exception
					setTitle("insert failed");
					e.printStackTrace();
				}
			}
		});
		
		bt_query.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				SQLiteDatabase dbDatabase = dbh.getReadableDatabase();
				String col[] = {TITLE,BODY};
				Cursor cursor=dbDatabase.query(TABLE_NAME, col, null,null, null
						,null, null);
				Integer num=cursor.getCount();
				
				setTitle(Integer.toString(num)+" records.");
			}
		});
		
	}
	
	
	
	class DataBaseHelper extends SQLiteOpenHelper{

		public DataBaseHelper(Context context) {
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
			// TODO Auto-generated constructor stub
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			// TODO Auto-generated method stub
			String sql="CREATE TABLE "+TABLE_NAME+"("+TITLE+
					" text not null,"+BODY+" text not null "+");";
			Log.i("table create :", sql);
			db.execSQL(sql);
		}
		
		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion,
				int newVersion) {
			// TODO Auto-generated method stub
			
		}
		
	}
	
	
}
